package com.example.automatic.abs_bank;

import android.accessibilityservice.AccessibilityService;
import android.os.Handler;
import android.text.TextUtils;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.example.automatic.abs_bank.base.BaseAccessibilityService;
import com.example.automatic.utils.LogUtils;

import java.util.List;

public class ZGAccessibilityService extends BaseAccessibilityService {

    private AccessibilityNodeInfo transCardNodeInfo, viewMoneyNodeInfo, transRemitPayeenameNodeInfo, clearEditContextNodeInfo, transRemitOpenbankNodeInfo;

    private Handler mHandler = new Handler();

    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {

            socketManager.sendMessage("hello world!");

            mHandler.postDelayed(this, 30 * 1000);
        }
    };

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        mHandler.postDelayed(mRunnable, 60 * 1000);
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {

//        AccessibilityNodeInfo accessibilityNodeInfo1 = getRootInActiveWindow();
//        if (accessibilityNodeInfo1 != null) {
//            LogUtils.e(event.getClassName() + "，" + accessibilityNodeInfo1.getClassName() + "，，，，" + event.toString());
//        }


        if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            if (event.getClassName().equals("com.boc.bocsoft.mobile.bocmobile.buss.transfer.common.activity.TransferActivity")) {
                AccessibilityNodeInfo titleValueTvNodeInfo =
                        getAccessibilityNodeInfo("com.chinamworld.bocmbci:id/titleValueTv");
                if (titleValueTvNodeInfo == null) {
                    return;
                }
                CharSequence cs = titleValueTvNodeInfo.getText();
                if (cs == null) {
                    return;
                }
                if (cs.equals("转账汇款")) {
                    LogUtils.e("=============转账汇款");
                } else if (cs.equals("账号转账")) {
                    LogUtils.e("=============账号转账");
                    AccessibilityNodeInfo transRemitPayeenameNodeInfo =
                            getAccessibilityNodeInfo("com.chinamworld.bocmbci:id/trans_remit_payeename");
                    if (transRemitPayeenameNodeInfo != null) {
                        changeInput(transRemitPayeenameNodeInfo, "林荔敏");
                    }
                    AccessibilityNodeInfo clearEditContextNodeInfo =
                            getAccessibilityNodeInfo("com.chinamworld.bocmbci:id/clear_edit_context");
                    if (clearEditContextNodeInfo != null) {
                        changeInput(clearEditContextNodeInfo, "6214921700959659");
                    }
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    AccessibilityNodeInfo transRemitOpenbankNodeInfo =
                            getAccessibilityNodeInfo("com.chinamworld.bocmbci:id/trans_remit_openbank");
                    if (transRemitOpenbankNodeInfo != null) {
                        AccessibilityNodeInfo choiceDataContentNodeInfo =
                                getAccessibilityNodeInfo("com.chinamworld.bocmbci:id/choice_data_content");
                        if (choiceDataContentNodeInfo == null) {
                            return;
                        }
                        if (TextUtils.isEmpty(choiceDataContentNodeInfo.getText()) || choiceDataContentNodeInfo.getText().equals("请选择")) {
                            transRemitOpenbankNodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        } else {
                            return;
                        }
                    }
                    affiliatedBank();
                } else if (cs.equals("选择所属银行")) {
                    affiliatedBank();
                }
            } else if (event.getClassName().equals("com.boc.bocsoft.mobile.bocmobile.base.widget.moneyinputview.MoneyInputDialog")) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String money = "50";
                        if (TextUtils.isEmpty(money)) {
                            return;
                        }
                        char[] chars = money.toCharArray();
                        for (char s : chars) {
                            try {
                                Thread.sleep(200);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            AccessibilityNodeInfo nodeInfo = getAccessibilityNodeInfoByText(String.valueOf(s));
                            nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        }
                        performGlobalAction(GLOBAL_ACTION_BACK);
                    }
                }).start();
            }
        }

//        if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
//            if (event.getClassName().equals("com.boc.bocsoft.mobile.bocmobile.buss.transfer.common.activity.TransferActivity")) {
//                AccessibilityNodeInfo transCardNodeInfo =
//                        getAccessibilityNodeInfo("com.chinamworld.bocmbci:id/layout_trans_card");
//                if (transCardNodeInfo != null) {
//                    this.transCardNodeInfo = transCardNodeInfo;
//                    return;
//                }
//                AccessibilityNodeInfo editContentNodeInfo =
//                        getAccessibilityNodeInfo("com.chinamworld.bocmbci:id/edit_content");
//                if (editContentNodeInfo != null) {
//                    changeInput(editContentNodeInfo, mTransferAccount.getOpening());
//                    return;
//                }
//
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        try {
//                            Thread.sleep(3000);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                        AccessibilityNodeInfo transRemitPayeenameNodeInfo =
//                                getAccessibilityNodeInfo("com.chinamworld.bocmbci:id/trans_remit_payeename");
//                        if (transRemitPayeenameNodeInfo != null) {
//                            changeInput(transRemitPayeenameNodeInfo, mTransferAccount.getName());
//                        }
//                        AccessibilityNodeInfo clearEditContextNodeInfo =
//                                getAccessibilityNodeInfo("com.chinamworld.bocmbci:id/clear_edit_context");
//                        if (clearEditContextNodeInfo != null) {
//                            changeInput(clearEditContextNodeInfo, mTransferAccount.getAccount());
//                        }
//                        AccessibilityNodeInfo viewMoneyNodeInfo =
//                                getAccessibilityNodeInfo("com.chinamworld.bocmbci:id/view_money");
//                        if (viewMoneyNodeInfo != null) {
//                            viewMoneyNodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
//                        }
//                        try {
//                            Thread.sleep(2000);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                        AccessibilityNodeInfo transRemitOpenbankNodeInfo =
//                                getAccessibilityNodeInfo("com.chinamworld.bocmbci:id/trans_remit_openbank");
//                        if (transRemitOpenbankNodeInfo != null) {
//                            transRemitOpenbankNodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
//                        }
//                    }
//                }).start();
//            } else if (event.getClassName().equals("com.boc.bocsoft.mobile.bocmobile.base.widget.moneyinputview.MoneyInputDialog")) {
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        String money = mTransferAccount.getMoney();
//                        if (TextUtils.isEmpty(money)) {
//                            return;
//                        }
//                        char[] chars = money.toCharArray();
//                        for (char s : chars) {
//                            try {
//                                Thread.sleep(200);
//                            } catch (InterruptedException e) {
//                                e.printStackTrace();
//                            }
//                            AccessibilityNodeInfo nodeInfo = getAccessibilityNodeInfoByText(String.valueOf(s));
//                            nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
//                        }
//                        performGlobalAction(GLOBAL_ACTION_BACK);
//                    }
//                }).start();
//            }
//        }

    }

    @Override
    public void onInterrupt() {

    }


    @Override
    public void onMessage(String text) {
        super.onMessage(text);
        LogUtils.e("开始转账------");
        mTransferAccount = new JSAccessibilityService.TransferAccount();
        mTransferAccount.setName("林荔敏");
        mTransferAccount.setOpening("光大银行");
        mTransferAccount.setAccount("6214 9217 0095 9659");
        mTransferAccount.setMoney("30");

        if (transCardNodeInfo == null) {
            return;
        }
        transCardNodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);

    }

    /**
     * 选择所属银行界面操作
     */
    private void affiliatedBank() {
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                AccessibilityNodeInfo editContentNodeInfo =
                        getAccessibilityNodeInfo("com.chinamworld.bocmbci:id/edit_content");
                if (editContentNodeInfo != null) {
                    changeInput(editContentNodeInfo, "光大银行");
                }

                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                AccessibilityNodeInfo accessibilityNodeInfo =
                        getAccessibilityNodeInfo("com.chinamworld.bocmbci:id/tv_bank_name", "中国光大银行");
                if (accessibilityNodeInfo != null) {
                    coordinateClick(accessibilityNodeInfo);
                }

                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                AccessibilityNodeInfo viewMoneyNodeInfo =
                        getAccessibilityNodeInfo("com.chinamworld.bocmbci:id/view_money");
                if (viewMoneyNodeInfo != null) {
                    viewMoneyNodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                }
            }
        }).start();
    }
}
